﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class EditRoleDialog : ChildWindow
    {
        public AddRoleDialog addRoleDialog;

        public EditRoleDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (tbRoleTitle.Text.Trim().Length > 0)
            {
                // Determine if the role already exists
                await Globals.db.GetRoleObjects("Title='" + tbRoleTitle.Text.Trim() + "'");
                if (Globals.db.RoleRecords.Count > 0) // Found record with the same name
                {
                    MessageBox.Show("Role title already exists.  Please choose a different title.");
                    return;
                }
                else
                {
                    // Update the role title
                    await Globals.db.GetRoleObjects("Title='" + addRoleDialog.cmbRole.SelectedValue.ToString() + "'");
                    Globals.db.RoleRecords[0].col_Title = tbRoleTitle.Text.Trim();
                    Globals.db.RoleRecords[0].bFilterTitle = true;
                    await Globals.db.UpdateRoleObject(Globals.db.RoleRecords[0]);
                    addRoleDialog.strSelectedValue = tbRoleTitle.Text.Trim();
                    await addRoleDialog.loadRoles();
                }                
                this.DialogResult = true;
            }
            else this.DialogResult = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbRoleTitle_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

